/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.control
{
	import mx.controls.Label;
	import mx.controls.dataGridClasses.DataGridListData;
	import mx.controls.listClasses.BaseListData;
	import mx.formatters.NumberFormatter;

	/**
	 * 数字渲染器.可格式化数字.如有效数字位数,千分符,四舍五入等. 
	 * 
	 */	
	public class NumberItemRender extends Label
	{
		private var _listData:DataGridListData;
		/****
		 * 是否使用千分位
		 */
		private var _useThousandsSeparator:Boolean = false;
		/****
		 * 保留的小数位数
		 */
		private var _precision:int = 2;
		/****
		 * none     直接截取
		 * up       向上四舍五入
		 * down     向下四舍五入
		 * nearest  向最近的取数字 
		 */
		private var _rounding:String = "up";
		private var _formatter:NumberFormatter;
		//private var _dataField:String;
		/****
		 * 参考@EDIDataGridCaculateColumn类中该属性
		 */
		private var _renderType:String = "node";
		
		private var _showCharAtFirst:String = "";
		
		private var _showCharAtLast:String = "";
		
		public function NumberItemRender()
		{
			super();
			_formatter = new NumberFormatter();
		}
		
		override public function set data(value:Object):void{
			var row:XML = value as XML;
			trace("row; "+row);
			if(row == null){
				return;
			}
			if(_listData.dataField == null){
				return;
			}
			if(_formatter != null){
				_formatter.useThousandsSeparator = this._useThousandsSeparator;
				_formatter.precision = this._precision;
				_formatter.rounding = this._rounding;
			}
			var fieldValue:String="";
			if(this._renderType == "attribute"){
				var fieldNode:XMLList = row[_listData.dataField];
				trace("fieldNode; "+fieldNode);
				if (!fieldNode){
						//this.text = dataField+"不存在";
						super.data = this.text;
						return;
				}
				fieldValue = fieldNode;
				fieldValue = fieldValue == "" ? "0":fieldValue;
				fieldValue = _formatter.format(Number(fieldValue));
				
			}else{
				var fieldNode:XMLList = row[_listData.dataField];
				trace("fieldNode; "+fieldNode);
				if (!fieldNode){
						//this.text = dataField+"不存在";
						super.data = this.text;
						return;
				}
				fieldValue = fieldNode.text();
				fieldValue = fieldValue == "" ? "0":fieldValue;
				fieldValue = _formatter.format(Number(fieldValue));
				
			}
			trace("text; "+text);
			text = fieldValue;
			trace("fieldValue; "+fieldValue);
			if(text.toLowerCase()=="nan"){
				text = "0.00";
			}
//			if(Number(text)>20){
//				this.setStyle("color","red");
//			}
			if(this._showCharAtFirst){
				text = this._showCharAtFirst+text;
			}
			if(this._showCharAtLast){
				text =text+ this._showCharAtLast;
			}
			
			super.data = text;
		}
		
		
		public function set useThousandsSeparator(value:Boolean):void{
			this._useThousandsSeparator = value;
		}
		public function get useThousandsSeparator():Boolean{
			return this._useThousandsSeparator;
		}
		
		public function set precision(value:int):void{
			this._precision = value;
		}
		public function get precision():int{
			return this._precision;
		}
		public function get renderType():String{
			return this._renderType;
		}
		
		public function set renderType(value:String):void{
			if(value)
				this._renderType = value;
		}
		
		public function set rounding(value:String):void{
			if(value)
				this._rounding = value;	
		}
		
		public function get rounding():String{
			return this._rounding;
		}
		
//		public function get dataField():String{
//			return this._dataField;
//		}
//		
//		public function set dataField(value:String):void{
//			if(value)
//				this._dataField = value;
//		}
		
		public function get showCharAtFirst():String{
			return this._showCharAtFirst;
		}
		
		public function set showCharAtFirst(value:String):void{
			if(value)
				this._showCharAtFirst = value;
		}
		
		public function get showCharAtLast():String{
			return this._showCharAtLast;
		}
		
		public function set showCharAtLast(value:String):void{
			if(value)
				this._showCharAtLast = value;
		}
		        /**
         * 在EDIDataGrid列表中的数据,包括行列位置信息等
         * @return 
         * 
         */        
        override public function get listData():BaseListData
		{
		    return _listData;
		}
		/**
		 * 在EDIDataGrid列表中的数据,包括行列位置信息等
		 * @param value
		 * 
		 */
		override public function set listData(value:BaseListData):void
		{
		    _listData = DataGridListData(value);
		} 
	}
}